Skip to content

Enter a GC free zone in shutdownLoop - #1

Merged
Aidan63 merged 3 commits into
Aidan63:mainfrom
Simn:gc-root-safety
Oct 29, 2026
Merged

Aidan63 merged 3 commits into
Aidan63:mainfrom
Simn:gc-root-safety

Conversation

@Simn

@Simn Simn commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Found while looking into crashes and hangs with hxcoro's LuvDispatcher (HaxeFoundation/hxcoro#164).

runLoop wraps uv_run in an AutoGCFreeZone, but shutdownLoop didn't. uv_run(UV_RUN_DEFAULT) can block there waiting for work items that are still running on libuv workers. If one of those triggers a collection, the collector waits forever for the blocked thread, which isn't in a free zone, and the process hangs. A stack dump of a hung hxcoro test run showed exactly this. With the free zone added, there were no hangs in 200 runs, against about 2% before. The free zone also makes the AutoGCZone in timer and async callbacks valid when they run during shutdown.

An earlier version of this PR also moved GC root removals out of free zones. Those changes only worked around hxcpp iterating the root set without its lock, so they've been dropped. That's fixed in HaxeFoundation/hxcpp#1438 instead.

Work.queue also registers and unregisters the worker thread for every work item. Registering each libuv worker once and having it wait in a GC-free zone between items would avoid that cost. I haven't made that change here.

🤖 Generated with Claude Code

Simn and others added 2 commits October 28, 2026 21:04
The GC iterates the root set without locking, and only stops registered
threads that are not in a GC free zone while doing so.

- Work: destroy the RootedObject before UnregisterCurrentThread.
- Timer and Async: leave the GC free zone in the uv_close callbacks, which
  run from uv_run inside runLoop's free zone, before removing the root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
uv_run can block while work items are still running on libuv worker threads.
If one of them triggers a collection, the collector waits for the blocked
thread forever, like runLoop already avoids with its AutoGCFreeZone. This also
makes the AutoGCZone in timer and async callbacks valid during shutdown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Simn Simn changed the title Only touch GC roots from registered threads outside GC free zones Fix GC zone handling in shutdownLoop and around GC roots Oct 28, 2026
Comment thread src/cpp/luv/HxcppLuvAsync.cpp Outdated
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* handle) {
auto data = static_cast<hx::Object**>(handle->data);

// Removing the root touches GC memory, which must not happen from a GC free zone.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this comment is right (or the wording is very vague at least), seems like the core issue is that the root set mutex isn't acquired before the GC marks and visits the roots. I'm not sure if that was a conscious decision, but it sounds like it should acquire it, I can't immediately think of any reason why non GC threads shouldn't be able to create and remove roots.

@Simn Simn Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the comment was wrong. GCAddRoot/GCRemoveRoot take sGCRootLock, but MarkAll and the visit path iterate sgRootSet/sgOffsetRootSet without it. Leaving the free zone only worked around that by waiting out any running collection. I've opened HaxeFoundation/hxcpp#1438, which takes the lock while iterating, and dropped the root changes here, so this PR is now only the shutdownLoop free zone fix.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we still need humans after all. At least for now...

The race they avoided comes from hxcpp walking the root set without
taking sGCRootLock, so it's being fixed in hxcpp instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Simn Simn changed the title Fix GC zone handling in shutdownLoop and around GC roots Enter a GC free zone in shutdownLoop Oct 29, 2026
@Aidan63
Aidan63 merged commit b87c215 into Aidan63:main Oct 29, 2026
@Simn
Simn deleted the gc-root-safety branch October 30, 2026 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants